package com.xzp.blog.controller;

import com.xzp.blog.controller.base.BaseController;
import com.xzp.blog.entity.User;
import com.xzp.blog.entity.dto.form.UserLoginForm;
import com.xzp.blog.entity.dto.form.UserRegisterForm;
import com.xzp.blog.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

import static com.xzp.blog.consts.ViewConsts.VIEW_MSG;

/**
 * 用户登陆控制器
 */
@Controller
public class UserController extends BaseController{

    @Autowired
    private IUserService userService;


//    @RequestMapping("/")          //完全可以这样配置,实现主页的跳转,不需要MVC框架
//    public String index(){
//        return "index";
//    }

//    @PostMapping("/test")
//    public String Test(HttpServletRequest request){
//        System.out.println(request.getHeader("xzp"));  //获取header中的参数
//        return "";
//    }

    /**
     * 前台用户登录
     * @param request
     * @param model
     * @param loginForm
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @PostMapping("/userlogin.f")
    public String fFrontUserLogin(HttpServletRequest request, Model model, @Valid UserLoginForm loginForm, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()){
            List<ObjectError> errors = bindingResult.getAllErrors();
            addModelAtt(model,VIEW_MSG,errors.get(0).getDefaultMessage());
            return "userlogin";
        }

        User user = userService.loginAuthentication(loginForm);
        if (user != null){
            userService.joinSession(request,user);
            return "redirect:/";
        }

        addModelAtt(model,VIEW_MSG,"用户名或密码错误");
        return "userlogin";
    }

    /**
     * 前台用户注册
     * 表单提交
     */

    @PostMapping("/userregister.f")
    public String fFrontUserRegister(@Valid UserRegisterForm registerForm,BindingResult bindingResult,HttpServletRequest request,Model model,User user){
        if (bindingResult.hasErrors()){
            List<ObjectError> errors =  bindingResult.getAllErrors();
            return "redirect:/userregister";
        }
        //再次进行重名校验
        if (userService.registerUsernameCheckExist(registerForm)) {
            return "redirect:/userregister";
        }
        //再次进行密码一致校验
        if (!(registerForm.getPassword().equals(registerForm.getConfirmpassword()))) {
            return "redirect:/userregister";
        }
        userService.insertUser(user);
        //跳转登录
        return "redirect:/userlogin";
    }

    /**
     * 注销登录
     *
     */

    @GetMapping("/usersignout.c")
    public String cFrontUserSignout(HttpServletRequest request) {
        userService.destroySession(request);
        return "redirect:index";
    }
}
